﻿using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.Options;

namespace Witt.AspNetCore.JMeter
{
    internal class ScriptMiddleware
    {
        private readonly RequestDelegate _next;
        private readonly JMeterScriptOptions _options;
        private readonly IActionDescriptorCollectionProvider _descriptorProvider;

        public ScriptMiddleware(RequestDelegate next,
            IOptions<JMeterOptions> options,
            IActionDescriptorCollectionProvider descriptorProvider)
        {
            _next = next;
            _options = options.Value.Script;
            _descriptorProvider = descriptorProvider;
        }

        public async Task InvokeAsync(HttpContext context)
        {
            var descriptors = _descriptorProvider.ActionDescriptors.Items.Cast<ControllerActionDescriptor>();

            await _next(context);
        }
    }
}
